package org.quadbac.converter;

import java.util.ArrayList;

import org.quadbac.measurements.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RemoveType extends Activity implements OnItemClickListener, DialogInterface.OnClickListener {
	private ListView listTypes;
	private ArrayList<MeasurementType> mTypes;
	int selTypeIndex;

	/** Called when the activity is first created. */
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.removetype);
		Bundle b = getIntent().getExtras();
		mTypes = (ArrayList<MeasurementType>) b.getSerializable("mTypes");
		listTypes = (ListView) findViewById(R.id.listTypes);
		listTypes.setAdapter(new ArrayAdapter<MeasurementType>(this,android.R.layout.simple_list_item_1,mTypes));
		listTypes.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		selTypeIndex = position;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Really remove "+mTypes.get(selTypeIndex).getTypeName()+"?");
		builder.setPositiveButton("Yes", this);
		builder.setNegativeButton("No", this);
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
 		switch (which){
		case DialogInterface.BUTTON_POSITIVE :
			getIntent().putExtra("selTypeIndex", selTypeIndex);
			setResult(RESULT_OK, getIntent());
			dialog.dismiss();
			finish();
			break;
		case DialogInterface.BUTTON_NEGATIVE :
			dialog.dismiss();
			break;
		}
	}
}
